package com.filmbooks.presenter.book.tabs;

import android.widget.ImageView;

import com.filmbooks.contract.book.BookCustomContract;
import com.filmbooks.contract.book.BookDetailActivity;
import com.filmbooks.model.book.tabs.BookCustomModel;
import com.filmbooks.model.home.bean.book.BookItemBean;
import com.filmbooks.model.home.bean.book.BookListBean;

import io.reactivex.functions.Consumer;

/**
 * Created by heshuxiang on 2018/3/6.
 * 描述：
 * 公司：盟将威
 * 作者：heshuxiang
 * 日期：2018/3/6.
 * 工程：FilmBooks
 * 包：com.filmbooks.presenter.book.tabs
 */

public class BookCustomPresenter extends BookCustomContract.BookCustomPresenter {


    private int mStart;
    private int mCount = 30;
    private boolean isLoading;

    @Override
    public BookCustomContract.IBookCustomModel getModel() {
        return BookCustomModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void loadLatestBookList() {
        if (mIModel == null || mIView == null)
            return;
        mStart = 0;
        mRxManager.register(mIModel.getBookListWitchTag(mIView.getBookTags(), mStart, mCount).subscribe(new Consumer<BookListBean>() {
            @Override
            public void accept(BookListBean bookListBean) throws Exception {
                if (mIView == null) {
                    return;
                }
                mStart += mCount;
                mIView.updateContentList(bookListBean.getBooks());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView == null)
                    return;

                if (mIView.isVisiable())
                    mIView.showToast("Network error.");

                mIView.showNetworkError();
            }
        }));
    }

    @Override
    public void loadMoreBookList() {
        if (!isLoading) {
            isLoading = true;
            mRxManager.register(mIModel.getBookListWitchTag(mIView.getBookTags(), mStart, mCount).subscribe(new Consumer<BookListBean>() {
                @Override
                public void accept(BookListBean bookListBean) throws Exception {
                    isLoading = false;
                    if (mIView == null)
                        return;
                    if (bookListBean != null && bookListBean.getBooks() != null &&
                            bookListBean.getBooks().size() > 0) {
                        mStart += mCount;
                        mIView.updateContentList(bookListBean.getBooks());
                    } else {
                        mIView.showNoMoreData();
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
                        mIView.showLoadMoreError();
                    }
                }
            }));
        }
    }

    @Override
    public void onItemClick(int position, BookItemBean item, ImageView imageView) {
        BookDetailActivity.start(mIView.getBindActivity(), item, imageView);
    }
}
